#pragma once
#include <mysql/mysql.h>
#include <iostream>
#include <stdlib.h>
#include <map> 
#include <list>
#include <string>
#include <time.h>
#include "../../CLogging.h"
#include "CDBValue.h"


#define P_DIR_IN 1
#define P_DIR_OUT 2
#define P_DIR_INOUT 3

#define P_TYPE_INT 1
#define P_TYPE_VARCHAR 2
#define P_TYPE_FLOAT 3
#define P_TYPE_TEXT 4
#define P_TYPE_TIMESTAMP 5

typedef std::map<std::string, CDBValue>  dbrow; 
typedef std::list<dbrow> dbrows;

    
class CNHDBAccess
{
  public:   
    CNHDBAccess(std::string server, std::string username, std::string password, std::string database, CLogging *log);
    ~CNHDBAccess();
    int dbConnect();
    void dbDisconnect();
    int exec_sp (std::string sp_name, int param_dir[], int param_type[], void **param_value, int param_length[], int param_count, dbrows *rs);
    void time_t2mysql(MYSQL_TIME *myTime, const time_t *cTime);
    
// {AUTOGENERATED-SP-DEFINITIONS}
    
  private:
      int process_results(MYSQL_STMT *stmt, dbrows *rs);
      int get_results(MYSQL_STMT *stmt, dbrows *rs, MYSQL_BIND *bind, 
                      unsigned long *length, my_bool *is_null, my_bool *error,
                      int field_count, MYSQL_RES *prepare_meta_result);
      int get_row(dbrows *rs, MYSQL_BIND *bind, int field_count, MYSQL_FIELD *fields);
      void free_bind_buffers(MYSQL_BIND *bind, int field_count);
      std::string server;
      std::string username;
      std::string password;
      std::string database;
      MYSQL mysql;
      MYSQL_RES *result; 
      CLogging *log;
      bool connected;
      pthread_mutex_t mysql_mutex;
};